home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
029a
/
masview.zip
/
TTT5.C
< prev
Wrap
Text File
|
1991-10-23
|
1KB
|
58 lines
/* ttt5.c - computers move */
#include "stdio.h"
#include "ttt.h"
extern char board[ 3 ][ 3 ]; /* main playing board */
computer_move()
{
char bd[ 3 ][ 3 ]; /* computers board for testing moves */
static struct { /* good moves in strategic order */
int row;
int col;
} gm[] = { 1,1, 0,0, 0,2, 2,0, 2,2, 0,1, 1,0, 1,2, 2,1 };
int x, y;
for ( x = 0; x < 3; ++x ) /* copy main board */
for ( y = 0; y < 3; ++y )
bd[ x ][ y ] = board[ x ][ y ];
for ( x = 0; x < 3; ++x ) /* win if I can */
for ( y = 0; y < 3; ++y )
if ( bd[ x ][ y ] == EMPTY )
{
bd[ x ][ y ] = COMPUTER;
if ( winner( bd ) )
{
cmove( x, y );
return;
}
bd[ x ][ y ] = EMPTY;
}
for ( x = 0; x < 3; ++x ) /* else stop human from winning */
for ( y = 0; y < 3; ++y )
if ( bd[ x ][ y ] == EMPTY )
{
bd[ x ][ y ] = HUMAN;
if ( winner( bd ) )
{
cmove( x, y );
return;
}
bd[ x ][ y ] = EMPTY;
}
for ( x = 0; x <= 8; ++x ) /* else pick best move available */
if ( board[ gm[ x ].row ][ gm[ x ].col ] == EMPTY )
{
cmove( gm[ x ].row, gm[ x ].col );
return;
}
}
static cmove( x, y )
int x, y;
{
board[ x ][ y ] = COMPUTER;
printf( "\nMy move is %c%c\n", x + '1', y + 'A' );
}